import os

from pypbc import *
path = os.path.dirname(__file__)
def setup():
    U = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

    q1 = get_random_prime(60)
    q2 = get_random_prime(60)

    params = Parameters(n=q1*q2)
    paramsfile = open(path+'/pairingparams.txt', 'w')
    paramsfile.write(str(params))

    # paramsfile = open('pairingparams.txt','r')
    # params_str = paramsfile.read()
    # params = Parameters(params_str)

    pairing = Pairing( params )
    g = Element.random(pairing,G1)

    MK = {}
    PK = {}

    y=Element.random(pairing,Zr)
    Y = (pairing.apply(g,g))**y #Y=e(g,g)**y #pbc.Elemnt


    MK['y']=y.__str__()
    PK['Y'] = Y.__str__()
    PK['g'] = g.__str__()

    #gs = Element(pairing,G1,value=g.__str__())
    #ys = Element(pairing,Zr,value=y.__int__())
    #Ys = Element(pairing,GT,value=Y.__str__())
    # print(Ys)
    # print(type(Ys))

    for i in range(1,len(U)+1):
        t = Element.random(pairing,Zr)
        T = Element(pairing,G1,value=g**t) #Ti = g**c
        MK['t'+str(i)]=t.__str__()
        PK['T'+str(i)]='0x'+T.__str__()
        print(t,t.__str__())
        print('t from str',Element(pairing,Zr,value=int(t.__str__(),16)))

    #publish PK on blockchian
    pkfile = open(path+'/PK.txt','w')
    pkstr = str(PK)
    pkfile.write(pkstr)
    pkfile.close()

    print(MK)
    print(PK)
    #store MK in localstorage
    mkfile = open(path+'/MK.txt', 'w')
    mkstr = str(MK)
    mkfile.write(mkstr)
    mkfile.close()

    #read MK
    # f = open('MK.txt','r')
    # mks = f.read()
    # mk = eval(mks)
    # f.seek(0)


setup()